988B - Substrings Sort - CodeForces Solution


sortings strings *1100

Please click on ads to support us..

Python Code:

qtd = int(input())
listat = list()
listap = list()
listar = list()
x = True

for i in range(0,qtd):
    p = input()
    listap.append(p)

listap = sorted(listap)


for i in range(0, len(listap)):
    word = listap[i]
    tam = len(word)
    listat.append(tam)

listat = sorted(listat)

for i in range(0, len(listap)):
    word = listap[i]
    tam = len(word)
    for j in range(0, len(listap)):
        if tam == listat[j]:
            listat[j] = word
            break


listar.append('YES')       
for i in range (0, qtd-1):
    primeira = listat[i]
    outra = listat[i+1]
    
    if outra.find(primeira) != -1:
        listar.append(primeira)
    else:
        print("NO")
        x = False
        break


if x == True:
    listar.append(listat[len(listat)-1])
    for i in range(0,len(listar)):
        print(listar[i])

		 		    	    		  	    		  	 		

C++ Code:

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> P;
#define all(x) (x).begin(), (x).end()
#define loop(a, n) for (ll a = 0; a < (n); ++a)
#define vloop(a, v) for (auto a = v.begin(); a != v.end(); ++a)
#define INF (ll)INFINITY
#define F first
#define S second
#define pb emplace_back
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;

bool check(string s, string t){
	if(s.size() == t.size() && s.compare(t) == 0) return true;
	loop(i, t.size() - s.size() + 1){
		string r = t.substr(i, s.size());
		if(s.compare(r) == 0) return true;
	}
	return false;
}
void solve(){
	int n;
	cin >> n;
	vector<string> st(n);
	loop(i, n) cin >> st[i];
	sort(all(st), [=](string s, string t){
	      if(s.size() == t.size()){
		      int k = 0;
		      while(k < s.size() && s[k] == t[k]) k++;
		      return s[k] < t[k];
	      }
	      return s.size() < t.size();
	});
	/* loop(i, n) cout << st[i] << " "; */
	loop(i, n-1){
		string s = st[i], t = st[i+1];
		/* cout << s << " " << t << " "; */
		if(!check(s, t)){
			cout << "NO";
			return;
		}
	}
	cout << "YES" << "\n";
	loop(i, n) cout << st[i] << "\n";
}

int main(){
	#ifndef ONLINE_JUDGE
	freopen("inp","r",stdin);
	#endif
	/* int t; */
	/* scanf("%d", &t); */
	/* while(t--) */
		solve();
}


Comments

Submit
0 Comments
More Questions

1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess